package Operator;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class Dleoperator implements IOPerator{


    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        int bookcount = bookList.getListsize();
        if(bookcount == 0){
            System.out.println("书架为空");
            return;
        }

        System.out.println("请输入你要删除的图书的名字");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int index = -1;
        for (int i = 0; i < bookcount; i++) {
            Book book = bookList.get_book(i);
            if(book.getName().equals(name)){
               index = i;
                break;
            }
        }
        if(index == -1){
            System.out.println("没有找到你要删除的书");
            return;
        }
        for (int i = index; i < bookcount-1; i++) {
            Book book1 = bookList.get_book(i+1);
            bookList.set_book(book1,i);
        }
        bookList.setListsize(bookcount-1);
        System.out.println("删除成功");
    }
}
